import java.io.File;
import java.net.URLDecoder;

import javax.media.*;
import javax.media.format.AudioFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
import javax.sound.sampled.AudioFileFormat;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import com.sun.speech.freetts.audio.SingleFileAudioPlayer;

public class WavManager {

    public static void GenerateDefaultMessage() {

        VoiceManager voiceManager = VoiceManager.getInstance();
        Voice defVoice = voiceManager.getVoice("kevin16");
        String def_filename = "default";
        SingleFileAudioPlayer def_audio = new SingleFileAudioPlayer(def_filename,AudioFileFormat.Type.WAVE);

        String defText = "This is my very brilliant SIP speaker and it welcomes you here!"
                     + "I hope to get a good grade for this stuff as it was actually a lot of work!"
                     + "Well, this is the default message as you did not specify the one to use!";

        defText = "This is my very brilliant SIP speaker and it welcomes you here!"
                     + "I hope to get a good grade for this stuff as it was actually a lot of work!"
                     + "Well, this is the default message as you did not specify the one to use!";

        defVoice.allocate();
        defVoice.setAudioPlayer(def_audio);

        def_audio.begin(10000);
        defVoice.speak(defText);
        def_audio.drain();
        def_audio.close();
        defVoice.deallocate();

        WebServer.webconf.default_message = "default.wav";
        SIPSpeaker.conf.default_message = "default.wav";
        WebServer.webconf.message_text = defText;
        SIPSpeaker.conf.message_text = defText;

    }

    public static void RecordMessage(String message) {
      
        try
        {
            String text = URLDecoder.decode(message.substring(8), "ISO-8859-15");
            System.out.println(text);

            VoiceManager voiceManager = VoiceManager.getInstance();
            Voice helloVoice = voiceManager.getVoice("kevin16");

            String out_filename = SIPSpeaker.conf.message_wav.split("\\.")[0];
            SingleFileAudioPlayer out_audio = new SingleFileAudioPlayer(out_filename,AudioFileFormat.Type.WAVE);

            helloVoice.allocate();
            helloVoice.setAudioPlayer(out_audio);

            out_audio.begin(10000);
            helloVoice.speak(text);
            out_audio.drain();
            out_audio.close();
            helloVoice.deallocate();

            SIPSpeaker.conf.message_text = text;
            WebServer.webconf.message_text = text;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        
    }

    public void PlayFileOnNet(String senderIpAddress, String RtpPort) throws Exception {

        Format[] format = new Format[] { new AudioFormat(AudioFormat.GSM_RTP) };
        ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);

        File mediaFile = new File(WebServer.webconf.message_wav);
        if (!mediaFile.exists())
        {
            mediaFile = new File(WebServer.webconf.default_message);
            if (!mediaFile.exists())
            {
                GenerateDefaultMessage();
            }
        }
        System.out.println("Transmitting file: "+mediaFile.getName());
        DataSource ds = Manager.createDataSource(new MediaLocator(mediaFile.toURL()));

        MediaLocator mlr = new MediaLocator("rtp://"+senderIpAddress+":"+RtpPort+"/audio");
        Processor mediaProcessor = Manager.createRealizedProcessor(new ProcessorModel(ds,format,cd));
        DataSink datasink = Manager.createDataSink(mediaProcessor.getDataOutput(),mlr);

        mediaProcessor.start();

        double wavDuration = mediaProcessor.getDuration().getSeconds();
        System.out.println("Duration of wav file: "+wavDuration+"\r\n");

        datasink.open();
        datasink.start();
        Thread.sleep(500+1000*(int)wavDuration);
        
        datasink.stop();
        datasink.close();
        mediaProcessor.stop();
        mediaProcessor.close();
    }
}